package com.jiepos.mpos.biz.customer.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.AppCustomerInfoDao;
import com.jiepos.mpos.common.dao.CustomLoginidChangeDao;
import com.jiepos.mpos.common.entity.App_Custom_Loginid_ChangeEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 
 * 会员登录账号变更业务类
 * 
 * @ProjectName: [mpos1]
 * @Package com.jiepos.mpos.biz.customer.service
 * @author 肖志伟
 * @version 1.0
 * @since 2017年9月26日 上午10:46:14
 * @history（历次修订内容、修订人、修订时间等） @UpdateRemark [说明本次修改内容]
 * @Copyright (c) 2017 江苏杰博实信息技术有限公司
 */
@Service
public class CustomLoginidChangeService extends BaseService {

	@Autowired
	private CustomLoginidChangeDao customLoginidChangeDao;
	@Autowired
	private AppCustomerInfoDao appCustomerInfoDao;

	/**
	 * 查询会员手机号变更json数据
	 * 
	 * @param request
	 * 
	 * @param pagination
	 * @param customLoginidChangeEntity
	 * @param name
	 * @param custom_no
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Map getCustomerAccountList(HttpServletRequest request, Pagination pagination,
			App_Custom_Loginid_ChangeEntity customLoginidChangeEntity, String name, String custom_no) {
		StringBuffer sb = getInitSql("t1").append(", ");
		sb.append("t2.custom_no, t1.change_desc, ");
		sb.append(
				"CONCAT (RPAD (SUBSTR (t1.old_login_id, 1, 3),LENGTH (t1.old_login_id) - 4,'*'),SUBSTR (t1.old_login_id,LENGTH (t1.old_login_id) - 3,4)) as old_login_id, ");
		sb.append(
				"CONCAT (RPAD (SUBSTR (t1.new_login_id, 1, 3),LENGTH (t1.new_login_id) - 4,'*'),SUBSTR (t1.new_login_id,LENGTH (t1.new_login_id) - 3,4)) as new_login_id, ");
		sb.append(
				"CONCAT (RPAD (SUBSTR (t2.login_id, 1, 3),LENGTH (t2.login_id) - 4,'*'),SUBSTR (t2.login_id,LENGTH (t2.login_id) - 3,4))||'-'||t2.name as name ");
		sb.append("from " + Table.T_APP_CUSTOM_LOGINID_CHANGE + " t1 ");
		sb.append("left join " + Table.T_APP_CUSTOM_INF + " t2 ");
		sb.append("on t1.custom_uuid = t2.custom_uuid ");
		sb.append("where 1=1 ");
		List list = new ArrayList();
		// 会员名称
		if (!StringUtils.isBlank(name)) {
			sb.append("and t2.name like ? ");
			list.add("%" + name + "%");
		}
		// 原手机号
		if (!StringUtils.isBlank(customLoginidChangeEntity.getOld_login_id())) {
			sb.append("and t1.old_login_id = ? ");
			list.add(customLoginidChangeEntity.getOld_login_id());
		}
		// 新手机号
		if (!StringUtils.isBlank(customLoginidChangeEntity.getNew_login_id())) {
			sb.append("and t1.new_login_id = ? ");
			list.add(customLoginidChangeEntity.getNew_login_id());
		}
		//会员编号
		if (!StringUtils.isBlank(custom_no)) {
			sb.append("and t2.custom_no = ? ");
			list.add(custom_no);
		}
		pagination.queryString = sb.toString();
		pagination = super.getCustomAuthSql(request, pagination, "t1", null);
		pagination = this.getOrderSql(pagination, "t1");
		return customLoginidChangeDao.getPaginationMap(pagination, "t1", list);

	}

	/**
	 * 根据会员编号查询
	 *
	 * @param new_login_id
	 * @return
	 */
	public boolean getPhone(String new_login_id, String login_id) {
		StringBuffer sqlBuf = new StringBuffer();
		sqlBuf.append(" select  ").append(" t1.* ").append(
				" from " + Table.T_APP_CUSTOM_LOGINID_CHANGE + " t1 where t1.old_login_id = ? or t1.new_login_id = ? ");
		List listParam = new ArrayList();
		listParam.add(new_login_id);
		listParam.add(new_login_id);
		List<Map<String, Object>> list = customLoginidChangeDao.queryForList(sqlBuf.toString(), listParam);
		if (list != null && list.size() > 0 && list.get(0) != null) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 插入变更手机号信息
	 * 
	 * @param custom_uuid
	 * @param login_id
	 * @param new_login_id
	 * @return
	 */
	public String instChangeLoginId(HttpServletRequest request, String custom_uuid, String login_id,
			String new_login_id, String change_desc) {
		String loginUser = String.valueOf(request.getSession().getAttribute(SystemConstant.SESSION_LOGIN_USER));
		Map map = new HashMap();
		map.put(SystemConstant.SESSION_LOGIN_USER, loginUser);
		map.put("request", request);
		App_Custom_Loginid_ChangeEntity appCustomLoginidChangeEntity = new App_Custom_Loginid_ChangeEntity();
		appCustomLoginidChangeEntity.setLoginid_change_uuid(IDFactoryUtil.getUUID());
		appCustomLoginidChangeEntity.setLast_oper_in(SystemConstant.OPER_ADD);
		appCustomLoginidChangeEntity.setCustom_uuid(custom_uuid);
		appCustomLoginidChangeEntity.setOld_login_id(login_id);
		appCustomLoginidChangeEntity.setNew_login_id(new_login_id);
		appCustomLoginidChangeEntity.setChange_desc(change_desc);
		appCustomLoginidChangeEntity.setRec_upd_usr_id("system");
		App_Custom_Loginid_ChangeEntity newappCustomLoginidChangeEntity = (App_Custom_Loginid_ChangeEntity) this
				.saveCommonData(appCustomLoginidChangeEntity, map, "null");
		customLoginidChangeDao.save(appCustomLoginidChangeEntity);
		return SystemConstant.RSP_CODE;
	}
}
